package com.apes.fn.scm.crossCompany.transfer.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.fn.scm.crossCompany.transfer.model.CrossCompanyTransferNew;
import com.apes.fn.scm.crossCompany.transfer.repository.CrossCompanyTransferNewRepository;
import com.apes.fn.scm.crossCompany.transfer.repository.CrossCompanyTransferRepository;
import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.jpa.spec.model.Filter;
import com.apes.framework.jpa.spec.model.Group;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.query.ApesDao;
import com.apes.framework.rop.session.SimpleSession;
import com.apes.framework.util.MapUtil;
import com.apes.scm.pur.purchaseOrder.model.PurchaseOrder;
import com.apes.scm.stock.transfer.model.Pick;
import com.apes.scm.stock.transfer.repository.PickRepository;
import com.apes.scm.stock.transfer.repository.TransferRepository;
import com.apes.scm.yy.QueryCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service("queryTransferService")
public class QueryTransferService extends ApesDao {
    @Autowired
    private EntityManager entityManager;
    @Autowired
    private QueryCommonService queryCommonService;
    @Autowired
    private CrossCompanyTransferNewRepository companyTransferNewRepository;
    @Autowired
    private CrossCompanyTransferRepository crossCompanyTransferRepository;
    @Autowired
    private TransferRepository transferRepository;

    public JSONObject queryCrossCompanyTransfer(SimpleRequest request) {
        JSONObject getJO = request.getJO();
        JSONArray conditionArray = getJO.getJSONArray("condition");
        SimpleSession getSeesion = request.getSession();
        JSONObject condition = new JSONObject();
        condition.put("page", getJO.getIntValue("page") + 1);
        condition.put("size", getJO.getIntValue("size"));
        condition.put("postId", getSeesion.getPostId());
        condition.put("branchId", getSeesion.getBranchId());
        if (StringUtils.hasText(getJO.getString("_orderField"))) {
            condition.put("_orderField", getJO.getString("_orderField").replaceAll(";", ","));
            condition.put("_orderType", getJO.getString("_orderType"));
        }
        conditionArray.forEach(conditionObj -> {
            JSONObject singleCondi = (JSONObject) conditionObj;
            if ("GTE".equals(singleCondi.getString("operator"))) {
                condition.put(singleCondi.getString("field") + "_START", singleCondi.get("value"));
            } else if ("LTE".equals(singleCondi.getString("operator"))) {
                condition.put(singleCondi.getString("field") + "_END", singleCondi.get("value"));
            } else {
                condition.put(singleCondi.getString("field"), singleCondi.get("value"));
            }
        });

        List result = this.queryCrossCompanyTransferSql(condition);
        JSONObject returnData = new JSONObject();
        returnData.put("total", this.queryCrossCompanyTransferSqlTotal(condition).size());
        returnData.put("pages", result.size());
        returnData.put("content", result);
        return returnData;
    }

    private List queryCrossCompanyTransferSql(JSONObject condition) {
        String sql = "" +
                "SELECT PAGE.* " +
                "  FROM (SELECT TAB.*, ROWNUM AS NUM " +
                "           FROM (";
        sql += this.crossCompanyTransferNewSql(condition);
        sql += "" +
                "                                  ) TAB " +
                "          WHERE ROWNUM <= (:page * :size)) PAGE " +
                " WHERE PAGE.NUM >= ((:page - 1) * :size) ";

        List result = this.find(sql, condition);
        return result;
    }

    private List queryCrossCompanyTransferSqlTotal(JSONObject condition) {
        String sql = "SELECT * FROM (";
        sql += this.crossCompanyTransferNewSql(condition);
        sql += ")";
        List result = this.find(sql, condition);
        return result;
    }

    private String crossCompanyTransferNewSql(JSONObject condition) {
        String sql = "SELECT ID, SHIP_LOCATION_ID, SHIP_LOCATION_NAME, " +
                "                         RECEIPT_LOCATION_ID, RECEIPT_LOCATION_NAME, PRODUCT_ID, " +
                "                         PRODUCT_NAME, " +
                "                         MATH_VALUE(PRODUCT_SUM, 'round', 2) AS PRODUCT_SUM, " +
                "                         SHIP_SUM, ARRIVA_SUM, FINISH_SUM, UOM, FACTOR, NOTE, " +
                "                         CREATOR_NAME, APPROVE_NAME, STATE, COMPANY_ID, " +
                "                         COMPANY_NAME, CREATE_DATE, DATE_APPROVE, PRINT_NUMBER " +
                "                    FROM (SELECT A.ID, A.SHIP_LOCATION_ID, " +
                "                                  (SELECT NAME " +
                "                                      FROM MAS_LOCATION " +
                "                                     WHERE ID = A.SHIP_LOCATION_ID) SHIP_LOCATION_NAME, " +
                "                                  A.RECEIPT_LOCATION_ID, " +
                "                                  (SELECT NAME " +
                "                                      FROM MAS_LOCATION " +
                "                                     WHERE ID = A.RECEIPT_LOCATION_ID) RECEIPT_LOCATION_NAME, " +
                "                                  B.PRODUCT_ID, " +
                "                                  (SELECT NAME FROM MAS_PRODUCT WHERE ID = B.PRODUCT_ID) PRODUCT_NAME, " +
                "                                  (SELECT BRAND_ID FROM MAS_PRODUCT WHERE ID = B.PRODUCT_ID) BRAND_ID, " +
                "                                  (SELECT CLASSIFY_ID FROM MAS_PRODUCT WHERE ID = B.PRODUCT_ID) CLASSIFY_ID, " +
                "                                  B.PRODUCT_SUM, B.SHIP_SUM, B.ARRIVA_SUM, " +
                "                                  B.FINISH_SUM, " +
                "                                  (SELECT NAME " +
                "                                      FROM MAS_PRODUCT_UOM " +
                "                                     WHERE MAS_PRODUCT_UOM.ID = B.PRODUCT_UOM_ID) UOM, " +
                "                                  1 / " +
                "                                   (SELECT FACTOR " +
                "                                      FROM MAS_PRODUCT_UOM " +
                "                                     WHERE MAS_PRODUCT_UOM.ID = B.PRODUCT_UOM_ID) FACTOR, " +
                "                                  NVL(A.NOTE, CHR(45) || CHR(45)) NOTE, " +
                "                                  NVL((SELECT D.NAME " +
                "                                         FROM MAS_PARTY_ROLE C, MAS_PARTY D " +
                "                                        WHERE C.ID = A.CREATOR_ID " +
                "                                          AND C.PARTY_ID = D.ID), CHR(45) || CHR(45)) CREATOR_NAME, " +
                "                                  NVL((SELECT D.NAME " +
                "                                         FROM MAS_PARTY_ROLE C, MAS_PARTY D " +
                "                                        WHERE C.ID = A.APPROVE_UID " +
                "                                          AND C.PARTY_ID = D.ID), CHR(45) || CHR(45)) APPROVE_NAME, " +
                "                                  DECODE(A.STATE, 'sent', '创建', 'apply', '申请', " +
                "                                          'approve', '交货中', 'done', '已完成', 'delete', " +
                "                                          '删除', '无') STATE, A.COMPANY_ID, " +
                "                                  (SELECT T.NAME " +
                "                                      FROM MAS_PARTY T, MAS_PARTY_ROLE P " +
                "                                     WHERE T.ID = P.PARTY_ID " +
                "                                       AND P.ID = A.COMPANY_ID) COMPANY_NAME, " +
                "                                  NVL(DATETIME(A.CREATE_DATE), CHR(45) || CHR(45)) CREATE_DATE, " +
                "                                  NVL(DATETIME(A.DATE_APPROVE), CHR(45) || CHR(45)) DATE_APPROVE, " +
                "                                  (SELECT P.PRINT_NUMBER " +
                "                                      FROM PRINT_RECORD P " +
                "                                     WHERE A.ID = P.SOURCE) PRINT_NUMBER " +
                "                             FROM CROSS_COMPANY_TRAN_NEW A, " +
                "                                  CROSS_COMPANY_TRAN_ITEM_NEW B, MAS_LOCATION C " +
                "                            WHERE A.ID = B.TRANSFER_ID " +
                "                              AND A.RECEIPT_LOCATION_ID = C.ID " +
                "                              AND A.STATE != 'delete' ";
        if (StringUtils.hasText(condition.getString("postId"))) {
            sql += "" +
                    "                              AND (((EXISTS " +
                    "                                   (SELECT 1 " +
                    "                                       FROM AUT_POSTS ROLE1, AUT_LOCAL_PERMIT ROLE2, " +
                    "                                            AUT_LOCAL_PERMIT_ITEM ROLE3 " +
                    "                                      WHERE ROLE1.LOCAL_PERMIT_ID = ROLE2.ID " +
                    "                                        AND ROLE2.ID = ROLE3.LOCAL_PERMIT_ID " +
                    "                                        AND ROLE1.ID = :postId " +
                    "                                        AND ROLE3.TYPE = 'pt_location' " +
                    "                                        AND ROLE3.PURPOSE = 'pp_search' " +
                    "                                        AND A.SHIP_LOCATION_ID = ROLE3.ORG_ID) OR " +
                    "                                   (SELECT NVL(COUNT(1), 0) " +
                    "                                       FROM AUT_POSTS ROLE1, AUT_LOCAL_PERMIT ROLE2, " +
                    "                                            AUT_LOCAL_PERMIT_ITEM ROLE3 " +
                    "                                      WHERE ROLE1.LOCAL_PERMIT_ID = ROLE2.ID " +
                    "                                        AND ROLE2.ID = ROLE3.LOCAL_PERMIT_ID " +
                    "                                        AND ROLE1.ID = :postId " +
                    "                                        AND ROLE3.TYPE = 'pt_location' " +
                    "                                        AND ROLE3.PURPOSE = 'pp_search') = 0) OR " +
                    "                                  (EXISTS " +
                    "                                   (SELECT 1 " +
                    "                                       FROM AUT_POSTS ROLE1, AUT_LOCAL_PERMIT ROLE2, " +
                    "                                            AUT_LOCAL_PERMIT_ITEM ROLE3 " +
                    "                                      WHERE ROLE1.LOCAL_PERMIT_ID = ROLE2.ID " +
                    "                                        AND ROLE2.ID = ROLE3.LOCAL_PERMIT_ID " +
                    "                                        AND ROLE1.ID = :postId " +
                    "                                        AND ROLE3.TYPE = 'pt_location' " +
                    "                                        AND ROLE3.PURPOSE = 'pp_search' " +
                    "                                        AND A.RECEIPT_LOCATION_ID = ROLE3.ORG_ID) OR " +
                    "                                   (SELECT NVL(COUNT(1), 0) " +
                    "                                       FROM AUT_POSTS ROLE1, AUT_LOCAL_PERMIT ROLE2, " +
                    "                                            AUT_LOCAL_PERMIT_ITEM ROLE3 " +
                    "                                      WHERE ROLE1.LOCAL_PERMIT_ID = ROLE2.ID " +
                    "                                        AND ROLE2.ID = ROLE3.LOCAL_PERMIT_ID " +
                    "                                        AND ROLE1.ID = :postId " +
                    "                                        AND ROLE3.TYPE = 'pt_location' " +
                    "                                        AND ROLE3.PURPOSE = 'pp_search') = 0)) " +
//                    "                              AND A.DATE_APPROVE >= " +
//                    "                                  TO_DATE('2021-10-28 00:00:00', " +
//                    "                                          'YYYY-MM-DD HH24:MI:SS') " +
//                    "                              AND A.DATE_APPROVE <= " +
//                    "                                  TO_DATE('2021-10-28 23:59:59', " +
//                    "                                          'YYYY-MM-DD HH24:MI:SS') " +
                    "                              OR ((EXISTS " +
                    "                                   (SELECT 1 " +
                    "                                       FROM AUT_POSTS ROLE1, AUT_LOCAL_PERMIT ROLE2, " +
                    "                                            AUT_LOCAL_PERMIT_ITEM ROLE3 " +
                    "                                      WHERE ROLE1.LOCAL_PERMIT_ID = ROLE2.ID " +
                    "                                        AND ROLE2.ID = ROLE3.LOCAL_PERMIT_ID " +
                    "                                        AND ROLE1.ID = :postId " +
                    "                                        AND ROLE3.TYPE = 'pt_company' " +
                    "                                        AND ROLE3.PURPOSE = 'pp_search' " +
                    "                                        AND A.COMPANY_ID = ROLE3.ORG_ID) OR " +
                    "                                   (SELECT NVL(COUNT(1), 0) " +
                    "                                       FROM AUT_POSTS ROLE1, AUT_LOCAL_PERMIT ROLE2, " +
                    "                                            AUT_LOCAL_PERMIT_ITEM ROLE3 " +
                    "                                      WHERE ROLE1.LOCAL_PERMIT_ID = ROLE2.ID " +
                    "                                        AND ROLE2.ID = ROLE3.LOCAL_PERMIT_ID " +
                    "                                        AND ROLE1.ID = :postId " +
                    "                                        AND ROLE3.TYPE = 'pt_company' " +
                    "                                        AND ROLE3.PURPOSE = 'pp_search') = 0) OR" +
                    "                                  (EXISTS " +
                    "                                   (SELECT 1 " +
                    "                                       FROM AUT_POSTS ROLE1, AUT_LOCAL_PERMIT ROLE2, " +
                    "                                            AUT_LOCAL_PERMIT_ITEM ROLE3 " +
                    "                                      WHERE ROLE1.LOCAL_PERMIT_ID = ROLE2.ID " +
                    "                                        AND ROLE2.ID = ROLE3.LOCAL_PERMIT_ID " +
                    "                                        AND ROLE1.ID = 'FM0260196019R001' " +
                    "                                        AND ROLE3.TYPE = 'pt_company' " +
                    "                                        AND ROLE3.PURPOSE = 'pp_search' " +
                    "                                        AND C.COMPANY_ID = ROLE3.ORG_ID) OR " +
                    "                                   (SELECT NVL(COUNT(1), 0) " +
                    "                                       FROM AUT_POSTS ROLE1, AUT_LOCAL_PERMIT ROLE2, " +
                    "                                            AUT_LOCAL_PERMIT_ITEM ROLE3 " +
                    "                                      WHERE ROLE1.LOCAL_PERMIT_ID = ROLE2.ID " +
                    "                                        AND ROLE2.ID = ROLE3.LOCAL_PERMIT_ID " +
                    "                                        AND ROLE1.ID = 'FM0260196019R001' " +
                    "                                        AND ROLE3.TYPE = 'pt_company' " +
                    "                                        AND ROLE3.PURPOSE = 'pp_search') = 0)))";


        }
        if (StringUtils.hasText(condition.getString("COMPOUND_ID"))) {
            sql += " AND (A.ID = :COMPOUND_ID OR A.SOURCE = :COMPOUND_ID)";
        }
        if (StringUtils.hasText(condition.getString("SHIP_LOCATION_ID"))) {
            sql += " AND A.SHIP_LOCATION_ID IN :SHIP_LOCATION_ID ";
        }
        if (StringUtils.hasText(condition.getString("RECEIPT_LOCATION_ID"))) {
            sql += " AND A.RECEIPT_LOCATION_ID IN :RECEIPT_LOCATION_ID ";
        }
        if (StringUtils.hasText(condition.getString("STATE"))) {
            sql += " AND A.STATE IN (:STATE) ";
        }
        if (StringUtils.hasText(condition.getString("PRODUCT_ID"))) {
            sql += " AND B.PRODUCT_ID = :PRODUCT_ID ";
        }
        if (StringUtils.hasText(condition.getString("DATE_APPROVE_START"))) {
            sql += " AND A.DATE_APPROVE >= TO_DATE('" + condition.getString("DATE_APPROVE_START") + "', 'YYYY-MM-DD HH24:MI:SS')";
        }
        if (StringUtils.hasText(condition.getString("DATE_APPROVE_END"))) {
            sql += " AND A.DATE_APPROVE <= TO_DATE('" + condition.getString("DATE_APPROVE_END") + "', 'YYYY-MM-DD HH24:MI:SS')";
        }
        if (StringUtils.hasText(condition.getString("CREATE_DATE_START"))) {
            sql += " AND A.CREATE_DATE >= TO_DATE('" + condition.getString("CREATE_DATE_START") + "', 'YYYY-MM-DD HH24:MI:SS')";
        }
        if (StringUtils.hasText(condition.getString("CREATE_DATE_END"))) {
            sql += " AND A.CREATE_DATE >= TO_DATE('" + condition.getString("CREATE_DATE_END") + "', 'YYYY-MM-DD HH24:MI:SS')";
        }
        if (StringUtils.hasText(condition.getString("PRINT_STATE")) && "1".equals(condition.getString("PRINT_STATE"))) {
            sql += " AND A.ID NOT IN (SELECT P.SOURCE FROM PRINT_RECORD P WHERE P.SOURCE_BUSINESS_TYPE = 'CrossCompanyTransferNew') ";
        }

        if (StringUtils.hasText(condition.getString("PRINT_STATE")) && "2".equals(condition.getString("PRINT_STATE"))) {
            sql += " AND A.ID IN (SELECT P.SOURCE FROM PRINT_RECORD P WHERE P.SOURCE_BUSINESS_TYPE = 'CrossCompanyTransferNew') ";
        }

        sql += "                                    AND ROWNUM <= 500000) A " +
                " WHERE 1 = 1 ";
        if (StringUtils.hasText(condition.getString("BRAND_ID"))) {
            sql += " AND A.BRAND_ID = :BRAND_ID ";
        }
        if (StringUtils.hasText(condition.getString("CLASSIFY_ID"))) {
            sql += " AND A.CLASSIFY_ID LIKE :CLASSIFY_ID || '%' ";
        }
        if (condition.getIntValue("PRINT_NUMBER") == 1) {
            sql += " AND A.PRINT_NUMBER IS NULL";
        } else if (condition.getIntValue("PRINT_NUMBER") == 2) {
            sql += " AND A.PRINT_NUMBER IS NOT NULL ";
        }
        if (StringUtils.hasText(condition.getString("_orderField"))) {
            sql += " ORDER BY " + condition.getString("_orderField") + " " + condition.getString("_orderType");
        }
        return sql;
    }


    public <T> T operatorInterfaceQueryCrossNew(SimpleRequest request) {
      return this.accordinNumberFindCross(request, companyTransferNewRepository);
    }
    public <T> T operatorInterfaceQueryCross(SimpleRequest request) {
        return this.accordinNumberFindCross(request, crossCompanyTransferRepository);
    }

    public <T> T operatorInterfaceQueryTransfer(SimpleRequest request) {
        return this.accordinNumberFindTransfer(request, transferRepository);
    }

    public <T> T accordinNumberFindCross(SimpleRequest simpleRequest, CustomRepository customRepository) {
        JSONObject jo = simpleRequest.getJO();
        JSONArray conditionArray = jo.getJSONArray("condition");
        JSONObject printStateObj = (JSONObject) conditionArray.stream().filter(singleCondi -> {
            Map condition = (Map) singleCondi;
            return "printState".equals(condition.get("field").toString());
        }).findFirst().orElse(new JSONObject());
        for (int i = 0; i < conditionArray.size(); i++) {
            Object condition = conditionArray.get(i);
            JSONObject condiObj = new JSONObject();
            if (condition instanceof HashMap) {
                condiObj.putAll((Map<? extends String, ? extends Object>) condition);
            } else {
                condiObj = (JSONObject) condition;
            }

            if (condiObj.getString("field").equals("customerExpand.id")) {
                List<String> fncustomerIds = this.findFnCustomerId(condiObj.getString("value"));
                if (fncustomerIds.isEmpty()) {
                    condiObj.put("value", "");
                } else {
                    condiObj.put("value", fncustomerIds);
                }
            }else if("terminal".equals(condiObj.getString("field"))){
                conditionArray.remove(i);
                i--;
            }
        }

        simpleRequest.setBody( jo.toString());

        int printState = -1;
        if (printStateObj.size() > 0) {
            printState = printStateObj.getInteger("value");
        }
        List<Map> columns = new ArrayList<Map>();
        Map columM = new HashMap<>();
        List customCondition = new ArrayList();
        customCondition.add(MapUtil.mapper("field", "shipLocation.id", "operator", "IN", "value", "%%"));
//        customCondition.add(MapUtil.mapper("field", "receiptLocation.id", "operator", "IN", "value", "%%"));


        List customCondition2 = new ArrayList();
        customCondition2.add(MapUtil.mapper("field", "id", "operator", "IN", "value", "%%"));
//        customCondition2.add(MapUtil.mapper("field", "specialDocuments", "operator", "IN", "value", "%%"));
        customCondition2.add(MapUtil.mapper("field", "source", "operator", "IN", "value", "%%"));

        List customCondition3 = new ArrayList();
        if (printState == 1) {
            customCondition3.add(MapUtil.mapper("field", "printNumber", "operator", "EQ", "value", "0"));
            columM.put("printState", customCondition3);
        } else if (printState == 2) {
            customCondition3.add(MapUtil.mapper("field", "printNumber", "operator", "GT", "value", "0"));
            columM.put("printState", customCondition3);
        } else if (printState == 0) {
            customCondition3.add(MapUtil.mapper("field", "printNumber", "operator", "GTE", "value", "0"));
            columM.put("printState", customCondition3);
        }
        columM.put("locationId", customCondition);
        columM.put("id", customCondition2);
        columns.add(columM);

        JSONObject getJO = jo;
        String excludesFields = "无".equals(getJO.getString("excludesFields"))?"":"purchaseItems";
        List fixedColumn = new ArrayList();
        fixedColumn.add("id");
        fixedColumn.add("locationId");
        fixedColumn.add("printState");
        if (customCondition.size() > 0) fixedColumn.add("company.id");

        JSONObject getData = simpleRequest.getData() instanceof JSONObject? (JSONObject)simpleRequest.getData():JSONObject.parseObject((String) simpleRequest.getData());
        getData.put("sort", "shipLocation asc,id asc");
        simpleRequest.setData(getData);
        Object obj = queryCommonService.accordinNumberFindLogic2(simpleRequest, customRepository, columns, fixedColumn, excludesFields);
        if ("java.util.HashMap".equals(obj.getClass().getName())) {
            return (T) obj;
        }
        Page page = queryCommonService.accordinNumberFindLogic2(simpleRequest, customRepository, columns, fixedColumn, excludesFields);
        return (T) page;
    }

    public <T> T accordinNumberFindTransfer(SimpleRequest simpleRequest, CustomRepository customRepository) {
        JSONArray conditionArray = simpleRequest.getJO().getJSONArray("condition");
        JSONObject printStateObj = (JSONObject) conditionArray.stream().filter(singleCondi -> {
            Map condition = (Map) singleCondi;
            return "printState".equals(condition.get("field").toString());
        }).findFirst().orElse(new JSONObject());

        conditionArray.stream().forEach(condition -> {
            JSONObject condiObj = condition instanceof HashMap? new JSONObject((Map) condition):(JSONObject) condition;
            if (condiObj.getString("field").equals("customerExpand")) {
                List<String> fncustomerIds = this.findFnCustomerId(condiObj.getString("value"));
                if (fncustomerIds.isEmpty()) {
                    condiObj.put("value", "");
                } else {
                    condiObj.put("value", fncustomerIds);
                }
            }
        });

        int printState = -1;
        if (printStateObj.size() > 0) {
            printState = printStateObj.getInteger("value");
        }
        List<Map> columns = new ArrayList<Map>();
        Map columM = new HashMap<>();
        List customCondition = new ArrayList();
        customCondition.add(MapUtil.mapper("field", "originLocation.id", "operator", "IN", "value", "%%"));


        List customCondition2 = new ArrayList();
        customCondition2.add(MapUtil.mapper("field", "id", "operator", "IN", "value", "%%"));
        customCondition2.add(MapUtil.mapper("field", "source", "operator", "IN", "value", "%%"));

        List customCondition3 = new ArrayList();
        if (printState == 1) {
            customCondition3.add(MapUtil.mapper("field", "printNumber", "operator", "EQ", "value", "0"));
            columM.put("printState", customCondition3);
        } else if (printState == 2) {
            customCondition3.add(MapUtil.mapper("field", "printNumber", "operator", "GT", "value", "0"));
            columM.put("printState", customCondition3);
        } else if (printState == 0) {
            customCondition3.add(MapUtil.mapper("field", "printNumber", "operator", "GTE", "value", "0"));
            columM.put("printState", customCondition3);
        }
        columM.put("locationId", customCondition);
        columM.put("id", customCondition2);
        columns.add(columM);

        JSONObject getJO = simpleRequest.getJO();
        String excludesFields = "无".equals(getJO.getString("excludesFields"))?"":"purchaseItems";
        List fixedColumn = new ArrayList();
        fixedColumn.add("id");
        fixedColumn.add("locationId");
        fixedColumn.add("printState");
        if (customCondition.size() > 0) fixedColumn.add("company.id");
        Object obj = queryCommonService.accordinNumberFindLogic2(simpleRequest, customRepository, columns, fixedColumn, excludesFields);
        if ("java.util.HashMap".equals(obj.getClass().getName())) {
            return (T) obj;
        }
        Page page = queryCommonService.accordinNumberFindLogic2(simpleRequest, customRepository, columns, fixedColumn, excludesFields);
        return (T) page;
    }


    // 获取赋能扩展表 id sql
    private List<String> findFnCustomerId(String carLicense) {
        String sql = "SELECT DISTINCT B.ID " +
                "  FROM CROSS_COMPANY_TRAN_NEW A, FN_CUSTOMER_EXPAND B " +
                " WHERE A.CUSTOMER_EXPAND_ID = B.ID " +
                "   AND B.CAR_LICENSE = '"+ carLicense + "'";
        return companyTransferNewRepository.findAll(sql);
    }
}
